package cn.ezios.huo.system.biz.user;

import cn.ezios.huo.system.biz.login.LoginEvent;
import cn.ezios.huo.system.biz.menu.MenuService;
import cn.ezios.huo.system.biz.menu.MenuVo;
import cn.ezios.huo.system.dao.domain.SysMenu;
import cn.ezios.huo.system.dao.service.SysMenuService;
import cn.ezios.huo.system.dao.service.SysRoleMenuService;
import cn.ezios.huo.system.dao.service.SysUserRoleService;
import cn.ezios.huo.system.dao.service.SysUserService;
import lombok.AllArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

import java.util.List;

import static cn.ezios.huo.system.biz.menu.MenuService.MENU_TYPE_CATALOG;

@Service
@AllArgsConstructor
public class InfoService {

    private final SysUserService sysUserService;

    private final SysMenuService sysMenuService;

    private final MenuService menuService;


    @EventListener
    public void initUserBaseInfo(LoginEvent loginEvent){
//        List<MenuVo> menuTree = getMenuTree(loginEvent);
//
//        StpUtil.getSession().set("menuTree", menuTree);
    }

    public List<MenuVo> getMenuTree(String userId) {
        List<String> menuIds = menuService.getMenuIdsByUserId(userId);
//        List<MenuVo> menuTree = menuService.getMenuTreeByMenuIds(menuIds);
        List<SysMenu> sysMenus = sysMenuService.listByIds(menuIds);
        List<MenuVo> menuTree = menuService.buildMenuTree(sysMenus);
        return menuTree;
    }

    /**
     * 获取用户的路由信息
     * @param userId
     * @return
     */
    public List<VueRoute> getUserVueRoute(String userId){
        List<String> menuIds = menuService.getMenuIdsByUserId(userId);
        List<SysMenu> sysMenus = sysMenuService.listByIds(menuIds);
        List<VueRoute> vueRoutes = sysMenus.stream()
                .filter(menu -> !menu.getMenuType().equals(MENU_TYPE_CATALOG) && menu.getComponent()!=null)
                .map(menu -> {
                    VueRoute route = new VueRoute();
                    route.setName(menu.getMenuName());
                    route.setPath(menu.getRouteUrl());
                    route.setComponent(menu.getComponent());
                    return route;
                })
                .toList();
        return vueRoutes;
    }




}
